package Lab5;

import java.util.Scanner;
import java.util.Stack;

public class LearnStack {
    public static boolean isPaired(String s) {
        Stack<Character> t = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '{' || c == '(' || c == '[') t.push(c);
            else if (c == '}' || c == ')' || c == ']') {
                if (t.isEmpty()) return false;
                char p = t.pop();
                if ((c == '}' && p != '{') || (c == ']' && p != '[') || (c == ')' && p != '('))
                    return false;
            }
        }
        return t.isEmpty();
    }

    public boolean isBalanced(String input) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else if (c == ')' || c == '}' || c == ']') {
                if (stack.isEmpty()) {
                    return false;
                }
                char top = stack.pop();
                if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '[')) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Input:");
        String input = scan.nextLine();
        System.out.println("Paired: " + isPaired(input));
    }

}
